package ch.usi.inf.pf2.cell;

/**
 * The objects of this class contains the value of the cell if the user has
 * inserted a formula
 * 
 * @author Lorenzo Baracchi, Thomas Mantegazzi
 */
public class Formula extends Data
{
	private String instance;
	private String actualValue;
	private boolean isError;
	private String errorMessage;
	private boolean computed;

	/**
	 * Empty useless constructor
	 */
	public Formula()
	{
		instance = "";
		// TODO set actualValue
		actualValue = instance;
		isError = false;
		errorMessage = null;
		computed = false;
	}

	/**
	 * Create a Formula object with the given instance
	 * 
	 * @param instance
	 *            the value for instance
	 * @return the new object
	 */
	public Formula(String instance)
	{
		this.instance = instance;
		// TODO set actualValue
		actualValue = instance;
		isError = false;
		errorMessage = null;
	}

	/**
	 * Return a string representation of the Instance object
	 * 
	 * @retrun the string
	 */
	public String toString()
	{
		return instance;
	}

	public String getActualValue()
	{
		return actualValue;
	}

	public boolean isThereError() {
		return isError;
	}

	public String getErrorMessage() {
		if(isError) {
			return errorMessage;
		}else {
			return null;
		}
	}
	
	public void setNoErrors() {
		this.isError = false;
		this.errorMessage = null;
	}
	
	public void setError(String errorMessage) {
		isError = true;
		this.errorMessage = errorMessage;
	}
	
	public boolean isComputed() {
		return computed;
	}
	
	public void setComputed(boolean b) {
		computed = b;
	}

	/**
	 * Compare this Formula to the given Data. The result is true only if d is
	 * an instance of Formula, is not null and has the same value for the field
	 * as this.
	 * 
	 * @param d
	 *            the data to compare
	 * @return true if they are equals, false otherwise
	 */
	public boolean equals(Data d)
	{
		if (d == null)
			return false;
		else if (d instanceof Formula)
			return equals((Formula) d);
		else
			return false;
	}

	/**
	 * Compare this Formula to the given Formula. The result is true only if f
	 * is not null and has the same value for the field as this.
	 * 
	 * @param f
	 *            the data to compare
	 * @return true if they are equals, false otherwise
	 */
	public boolean equals(Formula f)
	{
		if (f == null)
			return false;
		else
			return this.instance.equals(f.instance);
	}

	/**
	 * Return the instance value of the Formula
	 * 
	 * @return the String representing the value
	 */
	public String getInstance()
	{
		return instance;
	}

	/**
	 * Return the instance value of the Formula without =
	 * 
	 * @return the String representing the value
	 */
	public String getInstanceWithoutEqualSign()
	{
		return instance.replace("=", "");
	}

	/**
	 * Set the instance value to the given parameter
	 * 
	 * @param instance
	 *            the new value
	 */
	public void setInstance(String instance)
	{
		this.instance = instance;
	}

	/**
	 * Set the actualValue value to the given parameter
	 * 
	 * @param value
	 *            the new String value
	 */
	public void setActualValue(String value)
	{
		this.actualValue = value;
	}

	/**
	 * Set the instance value to the given parameter
	 * 
	 * @param value
	 *            the new int value
	 */
	public void setActualValue(int value)
	{
		this.actualValue = value + "";
	}

}